Java8 LocalDateTime to Joda LocalDateTime with jackson 您所在的位置:网站首页 date java Java8 LocalDateTime to Joda LocalDateTime with jackson

Java8 LocalDateTime to Joda LocalDateTime with jackson

2023-03-11 07:51| 来源: 网络整理| 查看: 265

Java8 LocalDateTime to Joda LocalDateTime with jackson

Yesterday I fought a problem that didn’t seem to take so much time that it actually did, so I decided to write a few lines to share the learned lesson.

I had the following case: a spring boot rest service, that returns a JSON object, and a consumer that reads this object using RestTemplate. All great but the fact this object contains java.time.LocalDateTime in the service that had to be transformed to org.joda.time.LocalDateTime in the consumer. I decided not to implement custom deserializer and here is the other way I managed to do it work solving lots of org.springframework.http.converter.HttpMessageNotReadableException

The service

The problem in the service was that the date in the result looked like

"departureTimeUTC": { "month": "MARCH", "year": 2017, "dayOfMonth": 8, "dayOfWeek": "WEDNESDAY", "dayOfYear": 67, "hour": 2, "minute": 40, "nano": 0, "second": 0, "monthValue": 3, "chronology": { "id": "ISO", "calendarType": "iso8601" } }

After adding the following maven dependency

com.fasterxml.jackson.datatype jackson-datatype-jsr310

And updating to false the write-dates-as-timestamps property in application.yml

spring: jackson: serialization: write-dates-as-timestamps: false

Got the nicer result

"departureTimeUTC": "2017-01-05T20:23:00"The client

In the client the following dependency had to be added

com.fasterxml.jackson.datatype jackson-datatype-joda

And then the RestTemplate had to “learn” about the JodaModule

RestTemplate restTemplate = new RestTemplate();ObjectMapper objectMapper = new ObjectMapper();objectMapper.registerModule(new JodaModule());List


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有